﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zzva.DomainObjectReg;
using Zzva.DomainObject;
using Zzva.Common;
using Zzva.Report;
using Zzva.ReportExcel;
using System.Collections;


namespace Zzva.UIWin
{
    public partial class TFrmRegDirSoftware : TFrmRegDirBase
    {

        TRegDirSoftware mvarRegDirSoftware;


        protected override void FilterByValue()
        {
            try
            {
                TFrmFilterByValue FrmFilterByValue;
                
                
                ////FrmFilterByValue = new TFrmFilterByValue((TRegDirBase)mvarRegDirSoftware, GetListAliasField());
                FrmFilterByValue = new TFrmFilterByValue((TRegDirBase)mvarRegDirSoftware, GetListAliasField(), GetCurColumnObozn());                
                
                
                FrmFilterByValue.EventReady += new EventHandler(FrmFilterByValue_Ready);
                FrmFilterByValue.ShowDialog();
            }

            catch (Exception e1) { throw e1; }
            finally { }
        }







































































        protected override void Report()
        {
            try
            {

                TReportBase lReportBase;

                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //Здесь достаемимя класса Отчета из установок
                //и по технологии Позднего связывания 
                //создаем объект Отчет
                //А пока так

                lReportBase = new TReportExcel();

                ////lReportBase.Title = mvarRegDirSoftware.Title;
                lReportBase.Title = mvarRegDirSoftware.TitleFull;

                object lList = mvarRegDirSoftware.GetCurListView();



                //lReportBase.CreateReport(lList);
                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //Здесь достаем парметр из установок:
                //Как мы хтим видеть поля отчета, представляющие объекты:
                //развернутыми по всем полям, или только строковое представление
                //А пока так по  умолчанию
                lReportBase.CreateReport(lList, EReportFieldView.ViewAsString);
                //lReportBase.CreateReport(lList, EReportFieldView.ExpandAsObject);

            }
            catch (Exception e1) { MessageBox.Show(e1.Message); }
            finally { }
        }


        protected override void ViewCurrentList()
        {
            try
            {
                object hhh = mvarRegDirSoftware.GetCurListView();

                bool lFlgStateLoadForm;
                lFlgStateLoadForm = mvarFlgStateLoadForm;
                if (mvarFlgStateLoadForm == false) { mvarFlgStateLoadForm = true; }

                dgvMain.DataSource = null;
                dgvMain.Rows.Clear();
                dgvMain.DataSource = hhh;

                DataGridViewColumn lDataGridViewColumn = dgvMain.Columns["Id"];
                lDataGridViewColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                lDataGridViewColumn.Resizable = DataGridViewTriState.False;
                //lDataGridViewColumn.ReadOnly = true;
                lDataGridViewColumn.Width = 0;
                //lDataGridViewColumn.Visible = false;


                mvarFlgStateLoadForm = lFlgStateLoadForm;


                TDirSoftware lCurItem;

                lCurItem = (TDirSoftware)mvarRegDirSoftware.GetCurItem();


                int lCurIndex;


                if (lCurItem != null)
                {
                    lCurIndex = GetIndexForId(dgvMain.Rows, lCurItem.Id);

                    if (lCurIndex == -1) { throw (new CommonException("Текущая запись не найдена")); }
                    dgvMain.CurrentCell = dgvMain.Rows[lCurIndex].Cells[mvarFlgColumnIndexCur];
                }


                ////
                this.Text = mvarRegDirSoftware.TitleFull;

            }
            catch (Exception e) { throw e; }
            finally { }
        }



        #region(constructor)
        public TFrmRegDirSoftware(TRegDirSoftware pRegDirSoftware)
            : base()
        {
            InitializeComponent();
            mvarRegDirSoftware = pRegDirSoftware;
            ViewCurrentList();


            if (mvarRegDirSoftware.CheckEventUpdateListIsNull() == true)
            {
                mvarRegDirSoftware.EventUpdateList += new EventHandler(RegDirBase_UpdateList);
            }


            if (mvarRegDirSoftware.CheckEventFindDirIsNull() == true)
            {
                mvarRegDirSoftware.EventFindDir += new EventHandler(RegDirBase_FindDir);
            }


        }
        #endregion


























































































































































































        protected override void Add()
        {
            try
            {
                TFrmDirSoftware FrmDirSoftware;

                ////FrmDirSoftware = new TFrmDirSoftware();
                TDirSoftwareNew lDirSoftwareNew;
                lDirSoftwareNew = (TDirSoftwareNew)mvarRegDirSoftware.GetTemplateNewItem();
                FrmDirSoftware = new TFrmDirSoftware(lDirSoftwareNew);
          

                FrmDirSoftware.EventReady += new EventHandler(FrmDirBase_Ready);
                
                ////FrmDirSoftware.Show();
                FrmDirSoftware.ShowDialog() ;

            }

            catch (Exception e1) { throw e1; }
            finally { }
        }
















        protected override void Ready()
        {
            try
            {

                ////TDirITCompany lDirITCompany = (TDirITCompany)mvarRegDirITCompany.GetCurItem();
                TDirSoftware lDirSoftware = (TDirSoftware)mvarRegDirSoftware.GetCurItem();


                TDirEventArgs lDirEventArgs;

                ////lDirEventArgs = new TDirEventArgs(lDirITCompany, lDirITCompany.Id);
                lDirEventArgs = new TDirEventArgs(lDirSoftware, lDirSoftware.Id);

                FireEventReady(lDirEventArgs);
                this.Close();
            }

            catch (Exception e) { MessageBox.Show(e.Message); }
            finally { }
        }


        protected override void FindPrev(TFilterValue pFindValue)
        {
            try{mvarRegDirSoftware.FindPrev(pFindValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }


        protected override void FindNext(TFilterValue pFindValue)
        {
            try{mvarRegDirSoftware.FindNext(pFindValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }



        protected override void Find(TFilterValue pFindValue)
        {
            try{mvarRegDirSoftware.Find(pFindValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }






        protected override void FilterAdd(TFilterValue pFilterValue)
        {
            try
            {mvarRegDirSoftware.FilterAdd(pFilterValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }

        protected override void FilterDel()
        {
            try
            {mvarRegDirSoftware.FilterDel();}
            catch (Exception e1) { throw e1; }
            finally { }
        }






        protected override void Sort(TSortValue pSortValue)
        {
            try{mvarRegDirSoftware.Sort(pSortValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }



        protected override void Delete()
        {
            try{mvarRegDirSoftware.Delete();}
            catch (Exception e1) { throw e1; }
            finally { }
        }





        protected override void FrmDirBase_Ready(object Sender, EventArgs ArgsBase)
        {
            try
            {
                int lId;
                TDirSoftwareNew lDir;
                TDirEventArgs lDirEventArgs;

                lDirEventArgs = (TDirEventArgs)ArgsBase;
                lId = lDirEventArgs.Id;
                lDir = (TDirSoftwareNew)lDirEventArgs.Dir;

                if (lId == 0) { AddDir(lDir); }
                else
                {
                    EditDir(lDir); 
                }
            }
            catch (Exception e) { throw e; }
            finally { }
        }



       private void EditDir(TDirSoftwareNew pDir)
        {
            try{mvarRegDirSoftware.Edit(pDir);}
            catch (Exception e1) { throw e1; }
            finally { }
        }



       protected override void Edit()
       {
           try
           {
               TFrmDirSoftware FrmDirSoftware;
               TDirSoftware lCurDir = (TDirSoftware)mvarRegDirSoftware.GetCurItem();
               FrmDirSoftware = new TFrmDirSoftware(lCurDir);
               FrmDirSoftware.EventReady += new EventHandler(FrmDirBase_Ready);
               
               ////FrmDirSoftware.Show();
               FrmDirSoftware.ShowDialog(); 


               //frmSub1.Visible = true;
               //frmSub1.ShowDialog(); 

           }

           catch (Exception e1) { throw e1; }
           finally { }
       }




        protected void AddDir(TDirSoftwareNew pDir)
        {
            try{mvarRegDirSoftware.Add(pDir);}
            catch (Exception e1) { throw e1; }
            finally { }
        }
















        protected override void SetCurItem(int pCurItem)
        {
            try { mvarRegDirSoftware.SetCurItem(pCurItem); }
            catch (Exception e1) { throw e1; }
            finally { }
        }










 






    }
}
